/**
 *
 *
 */

window.ginit = false;
window.ginitMenu = false;
window.ginitEvenMoreLink = false;
window.ginitIsPlus = false;

function create(tag, id, cl) {
    var el = document.createElement(tag);
    el.setAttribute('id', id);
    el.className = cl;
    return el;
}

function moveContentDown() {
    var moved = false;
    if (!window.ginitIsPlus) {
        if (!moved) {
            var cnt = snack.wrap('#cnt')[0];
            if (cnt && cnt.style) {
                cnt.style.top = "30px";
                moved = true;
            }
        }
        if (!moved) {
            var mainWrapper = snack.wrap('#main-wrapper')[0];
            if (mainWrapper && mainWrapper.style) {
                mainWrapper.style.marginTop = "30px";
                mainWrapper.ginit = true;
                moved = true;
            }
        }
        if (!moved) {
            var calcontent = snack.wrap('#calcontent')[0];
            if (calcontent && calcontent.style) {
                moved = true;
            }
        }
        if (!moved) {
            var page = snack.wrap('#page')[0];
            if (page && page.style) {
                page.style.top = "30px";
                moved = true;
            }
        }
        if (!moved) {
            var main = snack.wrap('#main')[0];
            if (main && main.style) {
                moved = true;
            }
        }
        if (!moved) {
            var doclist = snack.wrap('#doclist')[0];
            if (doclist && doclist.style) {
                doclist.style.top = "30px";
                moved = true;
            }
        }
        if (!moved) {
            var doclist = snack.wrap('#gt-c')[0];
            if (doclist && doclist.style) {
                doclist.style.top = "30px";
                moved = true;
            }
        }
        var gb = snack.wrap('#gb')[0];
        if (!moved) {
            if (gb && gb.parentNode) {
                var next = gb.parentNode.nextSibling;
                while (next && next.style && !moved) {
                    if (next.nodeName == 'DIV') {
                        next.style.marginTop = "30px";
                        moved = true;
                    } else {
                        next = next.nextSibling;
                    }
                }
            }
        }
        if (gb && gb.firstChild) {
            var fch = gb.firstChild;
            if (fch.nodeName == 'DIV') {
                fch.style.top = "30px";
            }
        }
    }
    return moved;
}

function initGmenuLinks() {
    if (!window.ginitIsPlus) {
        var gm_gbzc = snack.wrap('#gm_gbzc')[0];
        if (gm_gbzc.style) {
            var appLinks = snack.wrap('#gbwa a.gb_e');
            appLinks.each(function(element, index) {
                if (!element.ginit) {
                    element.ginit = true;
                    var id = element.getAttribute('id');
                    var text = element.lastChild.innerText;
                    var li = create('li', '', 'gm_gbt');
                    var a = create('a', 'gm_' + id, 'gm_gbzt');
                    a.setAttribute('href', '#');
                    var span1 = create('span', '', 'gm_gbtb2');
                    var span2 = create('span', '', 'gm_gbts');
                    gm_gbzc.appendChild(li);
                    li.appendChild(a);
                    a.appendChild(span1);
                    a.appendChild(span2);
                    span2.innerText = text;
                    a.setAttribute('onclick', "document.getElementById('" + id + "').click(); return false;");
                }
            });
        }
    }
}

function initGmenu() {
    var ol = document.getElementById('gbzc');
    var oldStyle = ol && ol.tagName == 'OL';
    window.ginit = window.ginit || oldStyle;
    window.ginitMenu = window.ginitMenu || oldStyle;
    window.ginitEvenMoreLink = window.ginitEvenMoreLink || oldStyle;
    window.ginitIsPlus = window.ginitIsPlus || oldStyle;
    if (!window.ginitMenu) {
        var gb = snack.wrap('#gb')[0];
        if (gb && gb.style) {
            var moved = moveContentDown();
            if (moved) {
                window.ginitMenu = true;
                var fch = gb.firstChild;
                fch.style.background = 'transparent';

                var gm_gbw = create('div', 'gm_gbw', '');
                var gm_gbzw = create('div', 'gm_gbzw', 'gm_gbes');
                var gm_gbz = create('div', 'gm_gbz', '');
                var span = create('span', '', 'gm_gbtcb');
                var gm_gbzc = create('ol', 'gm_gbzc', 'gm_gbtc');
                gb.appendChild(gm_gbw);
                gm_gbw.appendChild(gm_gbzw);
                gm_gbzw.appendChild(gm_gbz);
                gm_gbz.appendChild(span);
                gm_gbz.appendChild(gm_gbzc);

                var gm_gbx3 = create('div', 'gm_gbx3', 'gm_gbes');
                gb.appendChild(gm_gbx3);

                initGmenuLinks();
            }
        }
    } else {
        initGmenuLinks();
        moveContentDown();
    }
    if (!window.ginit) {
        snack.wrap('#gbwa div').each(function(element, index) {
            if (typeof (element.getAttribute) == 'function' && element.getAttribute("role") == "region") {
                element.style.width = "500%";
                element.style.height = "";
                var uls = element.getElementsByTagName('ul');
                if (uls && uls.length > 1) {
                    uls[0].setAttribute('style', 'width: 90%');
                    var lis = uls[1].getElementsByTagName('li');
                    snack.wrap(lis).each(function(element, index) {
                        try {
                            uls[0].appendChild(element);
                        } catch (ex) {
                            console.log(ex);
                        }
                    });
                }
                snack.wrap(element.getElementsByTagName('a')).each(function(link, index) {
                    if (link.className != "gb_e" && (link.className || "").indexOf('evenMoreLink') == -1) {
                        link.parentNode.removeChild(link);
                    }
                });
                if (!window.ginitEvenMoreLink) {
                    window.ginitEvenMoreLink = true;
                    snack.wrap('a.evenMoreLink').each(function(link, index) {
                        link.setAttribute('aria-hidden', 'false');
                        link.style.display = 'inline';
                        link.parentNode.style.width = "100%";
                        link.parentNode.style.textAlign = "center";
                    });
                }
            }
        });
    }
}

try {
    var base = document.getElementsByTagName('base')[0].getAttribute('href');
    window.ginitIsPlus = new RegExp('http[s]*://plus.google.*', 'ig').test(base);
} catch (ex) {
    window.ginitIsPlus = false;
    console.log(ex);
}

document.addEventListener('DOMNodeInserted', function() {
    if (window.gmenuTimeout) {
        clearTimeout(window.gmenuTimeout);
    }
    window.gmenuTimeout = setTimeout(initGmenu(), 5000);
});

//var d = document.createElement("div");
//d.innerHTML = "test";
//document.body.appendChild(d);